package netGaming;

import gameBasicElements.Position;
import gameEngine.GameStatus;

import java.io.*;

/**
 * @author Wojciech Łopata
 *
 */

public class ServerListener extends Thread
{
	private NetPlayer player;
	private ObjectInputStream input;
	
	public ServerListener(NetPlayer player, InputStream inputStream) throws IOException
	{
		this.setDaemon(true);
		this.player = player;
		input = new ObjectInputStream(inputStream);
	}
	
	@Override
	public void run()
	{
		while(player.StillInGame())
		{
			try
			{
				switch((ClientMessageType) input.readObject())
				{
					case MOVE_INFO:
						Position newPlayerMove = (Position) input.readObject();
						player.setMove(newPlayerMove);
						break;
				}
				
			} catch (Exception e)
			{
				player.changeStatus(GameStatus.LISTENER_INTRRUPTION);
			}
		}
		try
		{
			input.close();
		} catch (IOException e) { }
	}
}
